<script setup lang="ts">
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelSelect from '@/views/article/components/ChannelSelect.vue'
import { artGetListService, artDelService } from '@/apis/article/index'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const articleList = ref([]) //默认文本列表
const total = ref(0) //默认总条数
const articleEditRef = ref()
const loading = ref(false)
// 添加逻辑
const onAddArtice = () => {
  articleEditRef.value.open({})
}
//编辑逻辑
const onEditArticle = (row: any) => {
  articleEditRef.value.open(row)
}
//删除逻辑
const onDeleteArticle = async (row: any) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}
// 定义请求参数对象
const params = ref({
  pagenum: 1, //当前页
  pagesize: 10, //当前生效的每一页条数
  cate_id: '',
  state: ''
})
// 基于params参数获取文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()
//处理分页逻辑
const addSizeChange = (size: any) => {
  // console.log('当前每一页条数', size)
  //只要是每一页条数变了原本正在访问的当前页就意义不大了 数据大概率已经不在原来那一页了
  //重新从第一页渲染即可
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const addCurrentChange = (page: any) => {
  // console.log('页码变化了', page)
  // 更新当前页
  params.value.pagenum = page
  // 基于最新的当前页渲染数据
  getArticleList()
}
// 重置搜索的逻辑
// 搜索就是按照我们最新的条件重新检索 从第一页开始展示
const onsearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
// 重置就是将筛选条件清空 重新检索 从第一页开始展示
const onreset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
// 添加或者编辑成功的回调
const onsuccess = (type: any) => {
  if (type === 'add') {
    //如果是添加 最好渲染最后一页  相等于求得了现在有多少页 并且不带小数向上取整
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    // 更新成最大页码数
    params.value.pagenum = lastPage
  }

  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArtice">添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类:">
        <!-- v-model= :modelvalue 和 @update:modelvalue的简写 -->
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态:">
        <!-- 这里后台标记发布内容 就是通过中文标记的 已发布/草稿 -->
        <el-select style="width: 200px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onsearch" type="primary">搜索</el-button>
        <el-button @click="onreset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <!-- 如果是去处理一个数据不是做渲染而是做处理 不需要删除prop只需要在底下插入默认插槽上面的prop就会被忽略 如果将来需要prop就把默认插槽删除 -->
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button circle plain type="primary" :icon="Edit" @click="onEditArticle(row)"></el-button>
          <!-- 利用作用于插槽row可以获取当前行的数据就等于v-for里的item -->
          <el-button circle plain type="danger" :icon="Delete" @click="onDeleteArticle(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination v-model:current-page="params.pagenum" v-model:page-size="params.pagesize" :page-sizes="[2, 3, 5, 10]"
      :background="true" layout="jumper,total,sizes,prev,pager,next" :total="total" @size-change="addSizeChange"
      @current-change="addCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    <!-- 添加编辑的抽屉 -->
    <article-edit ref="articleEditRef" @success="onsuccess"></article-edit>
  </page-container>
</template>
<style lang="scss" scoped></style>
